package com.hos.check.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.hos.check.model.User;
import com.hos.check.services.IUserService;

@Controller  
@RequestMapping("/user.do")   
public class UserController {

	protected final transient Log log = LogFactory.getLog(UserController.class);
//	@Resource(name="userService")
	@Autowired
	private IUserService userService;

	@RequestMapping(params = "method=login")
	public ModelAndView login(HttpServletRequest request, ModelMap modelMap)
			throws Exception {
		String userName=request.getParameter("username");
		String password=request.getParameter("pwd");
		//User user=userService.findById(User.class, "12");
		
		List<User> user= userService.find(" from User u where  u.username=? ", userName);
		
		if(!user.isEmpty()){
			String pwd =user.get(0).getPassword();
			if(pwd.equals(password)){
				return new ModelAndView("index"); 
			}
		}
		//userService.save(u);
		return new ModelAndView("login"); 
		
//		String userName=request.getParameter("username");
//		String password=request.getParameter("pas");
//		String accessId=request.getParameter("accessId");
//		String dbId=request.getParameter("dpId");
//		boolean res=userService.checkLogin(userName, password, accessId, dbId,request);
//		if(res){
//			return new ModelAndView(new RedirectView("patient.do?method=list_patient")); 
//		}
//		else{
//			return new ModelAndView("login"); 
//		}
	}
//	
//	@RequestMapping(params = "method=loginOut")
//	public ModelAndView loginOut(HttpServletRequest request, ModelMap modelMap)
//			throws Exception {
//		
//		request.getSession().removeAttribute("loginUser");
//		return new ModelAndView(new RedirectView("login.jsp")); 
//	}


}
